
package com.iplant.basic.controller.bms;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BMSService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.bms.BMSRegion;

@RestController
@RequestMapping("/api/Region")
public class RegionController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(RegionController.class);

    @Autowired
    BMSService wBMSService;

    @GetMapping("/All")
    Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wTypeID = StringUtils.parseInt(request.getParameter("TypeID"));
            int wParentID = StringUtils.parseInt(request.getParameter("ParentID"));
            String wFullName = StringUtils.parseString(request.getParameter("FullName"));
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"));
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<BMSRegion>> wServiceResult = wBMSService.BMS_QueryRegionList(wLoginUser, wName,
                    wTypeID, wParentID, wFullName, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/Info")
    Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<BMSRegion> wServiceResult = wBMSService.BMS_QueryRegion(wLoginUser, wID, wCode);
            BMSRegion wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Update")
    Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BMSRegion wBMSRegion = CloneTool.Clone(wParam.get("data"), BMSRegion.class);
            wBMSRegion.EditorName = wUserName;
            wBMSRegion.EditorID = wUserID;
            wBMSRegion.EditTime = Calendar.getInstance();
            if (wBMSRegion.ID <= 0) {

                wBMSRegion.CreatorName = wUserName;
                wBMSRegion.CreatorID = wUserID;
                wBMSRegion.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wBMSService.BMS_UpdateRegion(wBMSEmployee, wBMSRegion);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wBMSRegion);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Delete")
    Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            BMSRegion wBMSRegion = CloneTool.Clone(wParam.get("data"), BMSRegion.class);

            ServiceResult<Integer> wServiceResult = wBMSService.BMS_DeleteRegion(wBMSEmployee, wBMSRegion);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Active")
    Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<BMSRegion> wBMSRegionList = CloneTool.CloneArray(wParam.get("data"), BMSRegion.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (BMSRegion wBMSRegion : wBMSRegionList) {

                wServiceResult = wBMSService.BMS_ActiveRegion(wBMSEmployee, wBMSRegion, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

}
